/**************************************************************************************
*  DemoDbg.java
*  Test code for the simpleRTJ Debugger.
**************************************************************************************
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL RTJ COMPUTING BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Created by:
*      RTJ Computing
**************************************************************************************/
import lpc2xxx.hw.*;

/**
 * Thread class for testing multithreaded debugging.
 */
class MyThread extends Thread
{
    String Name;
    boolean terminate = false;
    int count = 0;

    MyThread(String Name)
    {
        this.Name = new String(Name);
    }

    public void run()
    {
        DemoDbg.println(Name + " started");
        // To terminate this thread (if not created as daemon) set
        // "terminate" to true in the debugger while tracing this "while" loop
        while (!terminate)
        {
            count++;
            yield();
            if (Console.getKey() == Console.ESC)
                break;
        }
        DemoDbg.println(Name + " exited");
    }
}

/**
 * Test class containing the code to test the simpleRTJ Debugger.
 */
class DemoDbg
{
    static MyThread thr1, thr2;
    static String newLineString = System.getProperty("line.separator", "\r\n");
    static byte[] newLineBytes = newLineString.getBytes();

    static void main(String[] args)
    {
        if (args == null  || args.length == 0)
            args = init();

        // some text to print on the console
        println("Args count: "+args.length);
        for (int i=0; args != null && i < args.length; i++)
            println("  Args[" + i + "]: " + args[i]);

        thr1 = new MyThread("First");
        thr1.start();
        // If not set as daemon, the program should still run in MyThread.run method
        // when the main() exits.
        // Uncomment following line for program to exit when main() finishes.
//        thr1.setDaemon(true);

        thr2 = new MyThread("Second");
        thr2.start();
//        thr2.setDaemon(true);

        // boolean array to test the debugger's inspector/watch
        boolean[] boolArr = new boolean[8];
        for (int i=0; i < boolArr.length; i++)
            boolArr[i] = (i & 1) == 0 ? false : true;

        // short array to test the debugger's inspector/watch
        short[] shortArr = new short[10];
        for (int i=0; i < shortArr.length; i++)
            shortArr[i] = (short)(20 + i);

        // integer array to test the debugger's inspector/watch
        int[] intArr = new int[20];
        for (int i=0; i < intArr.length; i++)
            intArr[i] = 200 + i;

        // char array to test the debugger's inspector/watch
        char[] chArr = new char[15];
        for (int i=0; i < chArr.length; i++)
            chArr[i] = (char)('A' + i);
        chArr[0] = '\n';
        chArr[1] = '\r';
        chArr[2] = '\t';

        // float array to test the debugger's inspector/watch
        float[] fltArr = new float[11];
        for (int i=0; i < fltArr.length; i++)
            fltArr[i] = 3.55F + i;

        // String array to test the debugger's inspector/watch
        String[] strArr = new String[3];
        strArr[0] = new String("Sydney");
        strArr[1] = new String("Melbourne");
        strArr[2] = new String("Perth, Western Australia");

        // StringBuffer
        StringBuffer strBuf = new StringBuffer(strArr[0]);
        strBuf.append(", ");
        if (args.length > 0)
            strBuf.append(args[0]);

        // more code to test the inspector
        byte bVal = 10;
        bVal += 200;

        short sVal = 100;
        sVal += 40000;

        int iVal = 1000;
        iVal += 0xA0000000;

        float fVal = 2.35E-15F;
        fVal *= 45.444E19F;

        // in debugger let this while loop run and see if debugger catches
        // runtime exception in about 5 seconds
        // (breaks on run-time exception should be enabled in debugger).
        int loopCount = 0;
        String str = null;
        try
        {
            while (str == null)
            {
                iVal += 2;
                loopCount++;
                if (loopCount == 100)
                {
                    // this where null pointer exception should be thrown (str == null)
                    if (str.charAt(2) == 'a')
                        sVal += 100;
                }
                else
                    sVal += 10;

                try
                {
                    Thread.sleep(50);
                }
                catch (InterruptedException Exc) {}
            }
        }
        catch (Exception Exc)
        {
            println("Exception: " + Exc.getMessage());
        }

        // some try-catch-finally tests
        test();

        println("Lines printed: " + LineCount);
        println("Main exit");
    }

    // method nesting to test debugger's call stack retrieval
    static String[] init()
    {
        println("No args specified - crating some...");
        return createArgs(5);
    }

    // some try-catch-finally tests
    static void test()
    {
        int a = 0;
        try
        {
            try
            {
                throw new Exception("Hello");
            }
            catch (Exception Exc)
            {
                a = 3;
                return;
            }
            finally
            {
                a = 5;
            }
        }
        finally
        {
            a = 1;
        }
    }

    static String[] createArgs(int count)
    {
        String args[] = new String[count];
        for (int i=0; i < count; i++)
            args[i] = new String("Some string " + i*3);
        return args;
    }

    static int LineCount = 0;
    static void println(String s)
    {
        Console.println(s);
        LineCount++;
    }
}

